def call(Map map) {
    pipeline {
        agent any
        environment {
            GIT_BRANCH = "${map.GIT_BRANCH ?: 'master'}"
            HARBOR_REGISTRY_URL = "${map.HARBOR_REGISTRY_URL ?: 'harbor.zuolang.cn'}"
            HARBOR_PROJECT_NAME = "${map.HARBOR_PROJECT_NAME ?: 'go'}"
            RESOURCE_LIMITS_CPU = "${map.CPU ?: '100m'}"
            RESOURCE_LIMITS_MEM = "${map.MEMORY ?: '512Mi'}"
            RESOURCE_REQUESTS_CPU = "${map.CPU ?: '100m'}"
            RESOURCE_REQUESTS_MEM = "${map.MEMORY ?: '512Mi'}"
            DEPLOY_REPLICAS = "${map.REPLICAS ?: '1'}"
            TIMESTAMP = new Date().format("yyyyMMdd_HHmm")
            CGO_ENABLED = "0"  // 禁用 CGO 以生成静态二进制
        }
        parameters {
            string(name: 'GIT_BRANCH', defaultValue: map.BRANCH, description: '发布分支')
            choice(name: 'MODE', choices: ['DEPLOY', 'ROLLBACK'], description: '部署或回滚')
            choice(name: 'GIT_AUTH', choices: ['gitee'], description: '代码仓库凭据')
            choice(name: 'HARBOR_AUTH', choices: ['harbor'], description: '请选择产品镜像仓库凭据')
            booleanParam description: '重新部署：删除重建或者只更新镜像', name: 'REDEPLOY', defaultValue: false
            choice(name: 'NAMESPACE', choices: ['default'], description: 'K8s命名空间')
        }
        options {
            timeout(time: 1, unit: 'HOURS')
            timestamps()
        }
        stages {
            stage('代码拉取') {
                steps {
                    script {
                       def branch = params.BRANCH? params.BRANCH : env.GIT_BRANCH
                       checkout scmGit(
                        branches: [[name: branch]], 
                        extensions: [], 
                        userRemoteConfigs: [[credentialsId: 'gitee', url: map.GIT_URL]]) 
                    } 
                }
            }
            
            stage('容器镜像构建') {
                steps {
                    script {
                        withCredentials([usernamePassword(credentialsId: params.HARBOR_AUTH, usernameVariable: 'HARBOR_USER', passwordVariable: 'HARBOR_PASS')]) {
                            def tag = "${env.TIMESTAMP}_${env.BUILD_NUMBER}"
                            def image = "${env.HARBOR_REGISTRY_URL}/${env.HARBOR_PROJECT_NAME}/${map.SERVICE_NAME}:${tag}"
                            configFileProvider([configFile(fileId: 'go_dockerfile', targetLocation: 'Dockerfile',  variable: 'DOCKERFILE')]) {
                                // 执行镜像构建与推送
                                sh """
                                docker login -u ${HARBOR_USER} -p ${HARBOR_PASS} ${env.HARBOR_REGISTRY_URL}
                                docker build -t ${image} .
                                docker push ${image}
                                """
                            }
                        }
                    }
                }
            }
            stage('Helm 部署') {
                steps {
                    script {
                        sh """
                        if [ -d "./charts" ]; then
                            echo "目录 charts 已存在，跳过创建。"
                        else
                            mkdir charts
                        fi

                        if [ -d "./charts/${map.SERVICE_NAME}" ]; then
                            echo "目录 charts 已存在，跳过创建。"
                        else
                            helm create ./charts/${map.SERVICE_NAME}
                        fi
                        """
                        sh """
                        helm upgrade --install ${map.SERVICE_NAME} ./charts/${map.SERVICE_NAME} \
                        --version 1.0.0 \
                        --set imageRepository=${env.HARBOR_REGISTRY_URL}/${env.HARBOR_PROJECT_NAME}/${map.SERVICE_NAME} \
                        --set appVersion=${map.APP_VERSION} \
                        --set imageTag=${env.TIMESTAMP}_${env.BUILD_NUMBER} \
                        --set imagePullPolicy=Always \
                        --set replicaCount=${env.DEPLOY_REPLICAS} \
                        --set resources.limits.cpu=${env.RESOURCE_LIMITS_CPU} \
                        --set resources.limits.memory=${env.RESOURCE_LIMITS_MEM} \
                        --set resources.requests.cpu=${env.RESOURCE_REQUESTS_CPU} \
                        --set resources.requests.memory=${env.RESOURCE_REQUESTS_MEM} \
                        --set service.port=${map.PORT} \
                        --namespace=${params.NAMESPACE}
                        """
                    }
                }
            }
        }
    }
}